<?php
/**
 * Description of ProductDAO
 * implement 5 functions: get, getList, add, edit, delete
 * @author Huynh Vinh Loc
 */
class ProductDAO {    
    /*
     * Description of getProduct
     * find product by id
     * @ params $id product's ID
     * return the info of product
     */
    function getProduct($id){
        $product = new Product();
        $row = $product->fetchRow("id = $id and status =".Constant::$STATUS_AVAILABLE);
        return $row;
    }
    /*
     * Description of getProductList
     * return all the product available
     */
    function getProductList(){
       $product = new Product();
       $row = $product->fetchAll($product->select()->where('status = ?',Constant::$STATUS_AVAILABLE));
       return $row;
    }
    /*
     * Description of searchProduct
     * return rowset of product
     */
    function searchProduct($name){
        $product = new Product();
        $row = $product->fetchAll("status =".Constant::$STATUS_AVAILABLE." and name LIKE '%$name%'");
        return $row;
    }
    /*
     * Description of addProduct
     * add a new record
     */
    function addProduct($array){
        $product = new Product();
        $product->insert($array);
    }
    /*
     * Description of editProduct
     * edit info of product
     * add a new record, delete the old one
     */
    function editProduct($array){
        $product = new Product();
        $where = $product->getAdapter()->quoteInto('id = ?', $array['id']);
        $product->update($array,$where);
    }
    /*
     * Description of deleteProduct
     * change status to unavailable
     */
    function deleteProduct($id){
        $product = new Product();
        $data = array('status'=>Constant::$STATUS_DELETED); // change from unavailable to deleted
        $where = $product->getAdapter()->quoteInto('id = ?', $id);
        $product->update($data, $where);
    }    
}
?>
